﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BookManagement
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //2、定义sql命令
            string sql = "select * from y_Books";
            DataSet ds = dbHelper.GetData(sql);
            //6、将数据显示在gridview控件上
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //隐藏第一个窗体
            // this表示当前窗体
            this.Hide();
            //实例化窗体对象
            Form2 frm2 = new Form2();
            //显示窗体
            frm2.Show();
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //从前台获取用户输入的搜索数据
            string str = this.textBox1.Text;
            //2、定义sql命令
            string sql;
            if (string.IsNullOrEmpty(str))
            {
                sql = "select BookName,BookNum,Author,Introduce from y_Books";
            }
            else
            {
                sql = $"select BookName,BookNum,Author,Introduce from y_Books where BookName='{str}' or Author='{str}'";
            }
            DataSet ds = dbHelper.GetData(sql);
            //6、将数据显示在gridview控件上
            this.dataGridView1.DataSource = ds.Tables[0];
        }
    }
}
